package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.*;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.model.CompanyIntroduce;
import com.cz.zslu.model.StatisticsInfo;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 公司介绍 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-05-30
 */
@RestController
@RequestMapping("/sys/baseInfo")
@Api(description = "公司介绍管理")
public class CompanyBaseInfoController extends BaseController<CompanyBaseInfoService, CompanyBaseInfo> {

    @Autowired
    private CompanyBaseInfoService companyBaseInfoService;

    @Autowired
    private CompanyContactService companyContactService;

    @Autowired
    private HonorBookService honorBookService;

    @Autowired
    private IOriginAuthorizationService iOriginAuthorizationService;

    @Autowired
    private ISysUserService iSysUserService;

    @ApiOperation("获取详情")
    @IgnoreAuth
    @RequestMapping(value = "/findInfoByStoreId", method = RequestMethod.POST)
    public ReturnObject<CompanyBaseInfo> findInfoByStoreId() {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        CompanyBaseInfo baseInfo=baseService.selectOne(new EntityWrapper<CompanyBaseInfo>().eq("store_id", sysUser.getStoreId()));
        if(baseInfo==null){
            baseInfo=new CompanyBaseInfo();
        }
        return new ReturnObject<>(0, "获取成功", baseInfo);
    }

    @ApiOperation("新增/编辑（新增时 id为空）")
    @RequestMapping(value = "/saveBaseInfo", method = RequestMethod.POST)
    public ReturnObject<CompanyBaseInfo> saveIntroduce(@ApiParam("公司介绍") @RequestParam String companyIntroduce,
                                                       @ApiParam("公司封面") @RequestParam String companyCover,
                                                       @ApiParam("商铺id") @RequestParam Long storeId,
                                                       @ApiParam("id") @RequestParam(defaultValue = "0") Long id) {

        if (id != 0) {
            CompanyBaseInfo info = companyBaseInfoService.selectById(id);
            info.setCompanyIntroduce(companyIntroduce);
            info.setCompanyCover(companyCover);
            baseService.updateById(info);
        } else {
            CompanyBaseInfo info = new CompanyBaseInfo();
            info.setCompanyIntroduce(companyIntroduce);
            info.setCompanyCover(companyCover);
            info.setStoreId(storeId);
            baseService.insert(info);
        }
        return new ReturnObject<>(0, "新增成功", null);
    }


    @ApiOperation("获取公司介绍信息")
    @IgnoreAuth
    @RequestMapping(value = "/introduce", method = RequestMethod.POST)
    public ReturnObject<CompanyIntroduce> introduce(@ApiParam("商铺id") @RequestParam Long storeId) {
        CompanyIntroduce companyIntroduce = new CompanyIntroduce();
        //公司介绍
        CompanyBaseInfo baseInfo = companyBaseInfoService.selectOne(new EntityWrapper<CompanyBaseInfo>().eq("store_id",
                storeId));
        if (baseInfo == null) {
            baseInfo = new CompanyBaseInfo();
        }
        companyIntroduce.setCompanyBaseInfo(baseInfo);
        //详细信息
        CompanyContact contact = companyContactService.selectOne(new EntityWrapper<CompanyContact>().eq("store_id",
                storeId));
        if (contact == null) {
            contact = new CompanyContact();
        }
        companyIntroduce.setCompanyContact(contact);
        //荣誉资质
        List<HonorBook> honorBooks = honorBookService.selectList(new EntityWrapper<HonorBook>().eq(
                "store_id", storeId));
        companyIntroduce.setHonorBookList(honorBooks);
        //品牌证书
        //授权品牌
        List<OriginAuthorization> originAuthorizationList = iOriginAuthorizationService.selectList(new EntityWrapper<OriginAuthorization>().eq(
                "store_id", storeId).eq("status", 1));

        companyIntroduce.setOriginAuthorizations(originAuthorizationList);

        return new ReturnObject<>(0, "获取成功", companyIntroduce);
    }


    @ApiOperation("管理后台获取公司简介详情")
    @IgnoreAuth
    @RequestMapping(value = "/undergroundByStoreId", method =
            RequestMethod.POST)
    public ReturnObject<CompanyBaseInfo> undergroundByStoreId(@ApiParam("商铺id") @RequestParam Long storeId) {
        CompanyBaseInfo baseInfo=
                baseService.selectOne(new EntityWrapper<CompanyBaseInfo>().eq("store_id", storeId));
        if(baseInfo==null){
            baseInfo=new CompanyBaseInfo();
        }
        return new ReturnObject<>(0, "获取成功", baseInfo);
    }

}

